package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;

/**
 *
 * @author Jiazheng Sun
 */
public abstract class AbstractTransferController extends TmsController {

	/**
	 * 
	 */
	public abstract void doExecute(UserContext context) throws Exception;
	
	@Override
	public void execute(UserContext context) throws Exception {
		TxController controller = TxController.getTxController();

		//
		try {
			controller.begin();
			this.doExecute(context);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}

			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}

}
